package cn.Robbery.org.controller;

import cn.Robbery.basic.exception.BusinessException;
import cn.Robbery.basic.util.AjaxResult;
import cn.Robbery.basic.util.PageList;
import cn.Robbery.org.domain.Shop;
import cn.Robbery.org.query.ShopQuery;
import cn.Robbery.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "门店接口",description = "门店接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @PutMapping
    @ApiOperation(value = "门店添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试!"+ e.getMessage());
        }
    }


    @GetMapping("/{id}")  //http://localhost:8080/shop/1
    @ApiOperation(value = "查询一条门店数据",notes = "需要传入id")
    public Shop getById(@PathVariable("id") Long id){
        return shopService.getById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试!"+e.getMessage());
        }

    }

    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try{
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            System.out.println("-----");
            shopService.settledIn(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试!!"+e.getMessage());
        }
    }

}
